var completeKeyboard = null;
window.onload=initInput;
setUpEvent(document, "touchstart", function(e) {
        var elem = e.srcElement || e.target;
        var noNeedHideIds = ["CompleteKeyboard","password"];
        while(elem) {
            if(noNeedHideIds.indexOf(elem.id) !== -1) {
                return;
            }
            elem = elem.parentNode;
        }
        if (completeKeyboard.isShowing()) {
        	doneCallback("password")
        	completeKeyboard.hideKeyboard();
        }
    });

function doneCallback(sipboxId) {
	
	var length = $("input[name='"+sipboxId+"']").val().length;
	if(length < 6){
		BCSLayer.showMsg("密码长度不够6位");
		return false;
	}
	if(length > 20){
		BCSLayer.showMsg("密码长度不能超过20位");
		return false;
	}
/*	if(!completeKeyboard.checkMatchRegex(sipboxId))
	{
		BCSLayer.showMsg("至少包含数字和字母哟！");
		return false;
	}*/
	getEncrypt(sipboxId);
}
function initInput() {
	initCompleteKeyboard();
    initSipBoxComplete("password");
}
function setUpEvent(elem, eventType, handler) {
    return (elem.attachEvent ? elem.attachEvent("on" + eventType, handler)
            : ((elem.addEventListener) ? elem.addEventListener(eventType, handler, false) : null));
}

function initCompleteKeyboard() {
    if(completeKeyboard == null) {
    	completeKeyboard = new CFCAKeyboard("CompleteKeyboard", KEYBOARD_TYPE_COMPLETE);
    }
    completeKeyboard.setDoneCallback(doneCallback);
    completeKeyboard.bindInputBox("password");
    completeKeyboard.hideKeyboard();
    var serverRandom = $("input[name='serverRandom']").val();
    if(CFCA_OK != completeKeyboard.setServerRandom(serverRandom)) BCSLayer.showMsg("setServerRandom error");
}
    function initSipBoxComplete(sipboxId) {
    	setProperty(sipboxId);
        var sipBox1 = document.getElementById(sipboxId);
        setUpEvent(sipBox1, "focus",function(event) {
            sipBox1.blur();
            if(completeKeyboard.isShowing()) {
            	completeKeyboard.hideKeyboard();
            }
            completeKeyboard.showKeyboard();
        });
    }
function getKeyboard(sipboxId) {
     return completeKeyboard;
}
function setProperty(sipboxId) {
	var keyboard = getKeyboard(sipboxId);
    var outputType = OUTPUT_TYPE_ORIGINAL;
    var cipherType = CIPHER_TYPE_RSA;
    var minLength = 6;
    var maxLength = 20;
    //var matchRegex = "^(?=.*[0-9].*)(?=.*[a-zA-Z].*).{6,20}$";
    var serverRandom = $("input[name='serverRandom']").val();
    if(CFCA_OK != keyboard.setMinLength(minLength, sipboxId)) BCSLayer.showMsg("setMinLength error");
    if(CFCA_OK != keyboard.setMaxLength(maxLength, sipboxId)) BCSLayer.showMsg("setMaxLength error");
    if(CFCA_OK != keyboard.setOutputType(outputType, sipboxId)) BCSLayer.showMsg("setOutputType error");
    if(CFCA_OK != keyboard.setServerRandom(serverRandom, sipboxId)) BCSLayer.showMsg("setServerRandom error");
    if(CFCA_OK != keyboard.setCipherType(cipherType, sipboxId)) BCSLayer.showMsg("setCipherType error");
   // if(CFCA_OK != keyboard.setMatchRegex(matchRegex, sipboxId)) BCSLayer.showMsg("setMatchRegex error");

}
function getEncrypt(sipboxId) {
    var keyboard = getKeyboard(sipboxId);
    
    var pwdEncryptedData = document.getElementById("pwdEncryptedData");
    var encryptedInputValue = keyboard.getEncryptedInputValue(sipboxId);
    var errorCode = keyboard.getErrorCode(sipboxId).toString(16);
    if(errorCode != CFCA_OK) {
        pwdEncryptedData.value = "加密输入数据错误: 0x" + errorCode + "\n";
        return;
    } else{
        pwdEncryptedData.value = encryptedInputValue;
    }
    
    var rcEncryptedData = document.getElementById("rcEncryptedData");
    var encryptedClientRandom = keyboard.getEncryptedClientRandom(sipboxId);
    errorCode = keyboard.getErrorCode(sipboxId).toString(16);
    if(errorCode != CFCA_OK) {
        rcEncryptedData.value = "加密客户端随机数错误: 0x" + errorCode + "\n";
        return;
    } else{
        rcEncryptedData.value = encryptedClientRandom;
    }
}

function clearInput(sipboxId) {
    var keyboard = getKeyboard(sipboxId);
    keyboard.clearInputValue(sipboxId);
}
